#ifndef __ONLINE_H__
#define __ONLINE_H__
#include <mutex>
#include <cassert>
#include <unordered_map>
#include <websocketpp/server.hpp>
#include <websocketpp/config/asio_no_tls.hpp>
using wsserver_t = websocketpp::server<websocketpp::config::asio>;
class online_manager
{
public:
    void enter_game_hall(uint64_t id, wsserver_t::connection_ptr conn)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        _hall_user.insert(make_pair(id, conn));
    }
    void enter_game_room(uint64_t id, wsserver_t::connection_ptr conn)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        _room_user.insert(make_pair(id, conn));
    }
    void exit_game_hall(uint64_t id)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        _hall_user.erase(id);
    }
    void exit_game_room(uint64_t id)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        _room_user.erase(id);
    }
    bool is_in_game_hall(uint64_t id)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        auto it = _hall_user.find(id);
        if (it == _hall_user.end())
        {
            return false;
        }
        return true;
    }
    bool is_in_game_room(uint64_t id)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        auto it = _room_user.find(id);
        if (it == _room_user.end())
        {
            return false;
        }
        return true;
    }
    wsserver_t::connection_ptr get_conn_from_hall(uint64_t id)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        auto it = _hall_user.find(id);
        if (it == _hall_user.end())
        {
            return wsserver_t::connection_ptr();
        }
        return it->second;
    }
    wsserver_t::connection_ptr get_conn_from_room(uint64_t id)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        auto it = _room_user.find(id);
        if (it == _room_user.end())
        {
            return wsserver_t::connection_ptr();
        }
        return it->second;
    }

private:
    std::mutex _mutex;
    std::unordered_map<uint64_t, wsserver_t::connection_ptr> _hall_user;
    std::unordered_map<uint64_t, wsserver_t::connection_ptr> _room_user;
};

#endif